//
//  GTMapLoactionHandleProcess.m
//  zjkd
//
//  Created by app on 17/1/20.
//  Copyright © 2017年 gtexpress. All rights reserved.
//

#import "GTMapLoactionHandleProcess.h"


//当前索引页展示数量
int GTMapSearchOptionCurrentPage = 5;

//索引页分页数量
int  GTMapSearchOptionPageAmount = 10;

@interface GTMapLoactionHandleProcess ()<BMKLocationServiceDelegate,BMKPoiSearchDelegate,BMKGeoCodeSearchDelegate>

//地理编码，Geo检索
@property (nonatomic,strong)BMKGeoCodeSearch * bmkGeoCodeSearch;

//反地理编码检索
@property (nonatomic,strong)BMKReverseGeoCodeOption * reverseGeoCodeOption;

@end

@implementation GTMapLoactionHandleProcess


- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

//开始检索
- (void)startToPoiNearByExpressCompany
{
    self.locationSever.distanceFilter = 500.0f;
    self.locationSever.delegate = self;
    [self.locationSever startUserLocationService];
}

- (void)startToPoiNeayByWithSendInfoCoordinate:(CLLocationCoordinate2D)coordinate andKeyword:(NSString *)keyword
{
    //中国区不存在latitude == 0
    if(coordinate.latitude == 0)
    {
        return;
    }
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageIndex = GTMapSearchOptionCurrentPage;
    option.pageCapacity = GTMapSearchOptionPageAmount;
    option.location = coordinate;
    option.keyword = keyword;
    BOOL flag = [self.bmkSearcher poiSearchNearBy:option];
    if(flag)
    {
        NSLog(@"周边检索发送成功");
        self.currentCoordinate = coordinate;
    }
    else
    {
        NSLog(@"周边检索发送失败");
        if (self.mapDelegate && [self.mapDelegate respondsToSelector:@selector(mapProcessHandleWithDataSource:)]) {
            
            [self.mapDelegate mapProcessHandleFailWithType:GTMapHandleFaulurePoiSearchSendFail];
        }
        
    }
    

}

- (void)startToPoiNeayByWithSendInfoCoordinate:(CLLocationCoordinate2D)coordinate
{
    [self startToPoiNeayByWithSendInfoCoordinate:coordinate andKeyword:@"快递"];
}

//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    //NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    //发起检索
    
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageIndex = GTMapSearchOptionCurrentPage;
    option.pageCapacity = GTMapSearchOptionPageAmount;
    option.location = userLocation.location.coordinate;
    option.keyword = @"快递";
    BOOL flag = [self.bmkSearcher poiSearchNearBy:option];
    if(flag)
    {
        NSLog(@"周边检索发送成功");
        
        self.currentCoordinate = userLocation.location.coordinate;
    }
    else
    {
        NSLog(@"周边检索发送失败");
        if (self.mapDelegate && [self.mapDelegate respondsToSelector:@selector(mapProcessHandleWithDataSource:)]) {
            
            [self.mapDelegate mapProcessHandleFailWithType:GTMapHandleFaulurePoiSearchSendFail];
        }
    }
//    [self reverseGeoCodeWithLocation:userLocation.location];
//
}

//实现PoiSearchDeleage处理回调结果
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
    
    [self.poiResultDataSource removeAllObjects];
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        
        BMKMapPoint currentPoint = BMKMapPointForCoordinate(self.currentCoordinate);
       // NSLog(@"%f-%f",currentPoint.x,currentPoint.y);

        
        for (int i = 0; i < poiResultList.poiInfoList.count; i++) {
            GTNearByExpressCompanyModel * model = [[GTNearByExpressCompanyModel alloc]init];
            
            BMKPoiInfo *info = [poiResultList.poiInfoList objectAtIndex:i];
            //NSLog(@"地址：%@,电话：%@,名字：%@", info.address,info.phone,info.name);
            
            model.currentLocationCoordinate = self.currentCoordinate;
            model.resultLocationCoordinate = info.pt;
            model.poiResultAddress = info.address;
            model.poiResultName = info.name;
            model.poiResultPhoneNumber = info.phone;
            BMKMapPoint resultPoint = BMKMapPointForCoordinate(info.pt);
          //  NSLog(@"%f-%f",resultPoint.x,resultPoint.y);
            
            CLLocationDistance  distance = BMKMetersBetweenMapPoints(currentPoint, resultPoint);
        //    NSLog(@"distance = %f",distance);
            model.poiResultDistance = [NSString stringWithFormat:@"%f",distance];
            [self.poiResultDataSource addObject:model];
        }
        
        
        if (self.mapDelegate && [self.mapDelegate respondsToSelector:@selector(mapProcessHandleWithDataSource:)]) {
            
            [self.mapDelegate mapProcessHandleWithDataSource:self.poiResultDataSource];
        }
        
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        [self.locationSever stopUserLocationService];
        //当在设置城市未找到结果，但在其他城市找到结果时，回调建议检索城市列表
        // result.cityList;
        //[AnimationView stop];
        if (self.mapDelegate && [self.mapDelegate respondsToSelector:@selector(mapProcessHandleWithDataSource:)]) {
            
            [self.mapDelegate mapProcessHandleFailWithType:GTMapHandleFailureWithAmbiguous];
        }

        NSLog(@"起始点有歧义");
    } else {

        [self.locationSever stopUserLocationService];
        if (self.mapDelegate && [self.mapDelegate respondsToSelector:@selector(mapProcessHandleWithDataSource:)]) {
            
            [self.mapDelegate mapProcessHandleFailWithType:GTMapHandleFailureWithNoResult];
        }
        NSLog(@"抱歉，未找到结果");
    }
}

//调用反地理编码
- (void)reverseGeoCodeWithLocation:(CLLocation *)location
{
    self.reverseGeoCodeOption.reverseGeoPoint = location.coordinate;
   BOOL geoSuccess =  [self.bmkGeoCodeSearch reverseGeoCode:self.reverseGeoCodeOption];
    if (geoSuccess) {
        
        NSLog(@"success");
    }else
    {
        NSLog(@"fail");
    }
    
    

}

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    
    for (BMKPoiInfo *poi in result.poiList) {
       // NSLog(@"当前位置名称%@",poi.name);//周边建筑名
      //  NSLog(@"当前位置 = %f - %f",poi.pt.latitude,poi.pt.longitude);
        
    }

}

- (void)endPoiSearch
{
    [self.locationSever stopUserLocationService];
    self.locationSever.delegate = nil;
}

#pragma mark  getter and setter
-(NSMutableArray *)poiResultDataSource
{
    if (!_poiResultDataSource) {
        _poiResultDataSource = [[NSMutableArray alloc]init];
    }
    return _poiResultDataSource;
}
- (BMKLocationService *)locationSever
{
    if (!_locationSever) {
        _locationSever =[[BMKLocationService alloc]init];
        _locationSever.delegate = self;
    }
    
    return _locationSever;
}

- (BMKReverseGeoCodeOption *)reverseGeoCodeOption
{
    if (!_reverseGeoCodeOption) {
        _reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc]init];
    }
    return _reverseGeoCodeOption;
}

- (BMKPoiSearch *)bmkSearcher
{
    if (!_bmkSearcher) {
        _bmkSearcher = [[BMKPoiSearch alloc]init];
        _bmkSearcher.delegate = self;
    }
    return _bmkSearcher;
}

- (BMKGeoCodeSearch *)bmkGeoCodeSearch
{
    if (!_bmkGeoCodeSearch) {
        _bmkGeoCodeSearch = [[BMKGeoCodeSearch alloc]init];
        _bmkGeoCodeSearch.delegate = self;
    }
    return _bmkGeoCodeSearch;
}







@end
